function maxSumDivThree(nums) {
  let dp0 = 0,
    dp1 = -Infinity,
    dp2 = -Infinity;
  for (const num of nums) {
    if (num % 3 === 0) {
      dp0 = Math.max(dp0, dp0 + num);
      dp1 = Math.max(dp1, dp1 + num);
      dp2 = Math.max(dp2, dp2 + num);
    } else if (num % 3 === 1) {
      const newDp0 = Math.max(dp0, dp2 + num);
      const newDp1 = Math.max(dp1, dp0 + num);
      const newDp2 = Math.max(dp2, dp1 + num);
      dp0 = newDp0;
      dp1 = newDp1;
      dp2 = newDp2;
    } else {
      // 余2
      const newDp0 = Math.max(dp0, dp1 + num);
      const newDp1 = Math.max(dp1, dp2 + num);
      const newDp2 = Math.max(dp2, dp0 + num);
      dp0 = newDp0;
      dp1 = newDp1;
      dp2 = newDp2;
    }
  }
  return dp0 > 0 ? dp0 : 0;
}
